// pages/me/me.js
const util = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:{},
    state: { State0: 0, State1: 0, State2: 0, State3: 0, State5: 0}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.GoLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    this.setData({user:app.User});
    this.Find_StateCount();
    this.Find_UserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  openMyorder:function(e){
    var state = e.currentTarget.dataset.state;
    wx.navigateTo({
      url: '/pages/myorder/myorder?state=' + state
    })
  },
  openStore:function(e){
    var select = e.currentTarget.dataset.select;
    wx.navigateTo({
      url: '/pages/store/store?select=' + select
    })
  },
  openQuan:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/quan/quan?id=' + id
    })
  },
  openNotice: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/notice/notice?id=' + id
    })
  },
  openScore: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/score/score?id=' + id
    })
  },
  openAddress: function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/address/address?id=' + id
    })
  },
  Find_StateCount: function () {
    var that = this;
    var data = {};
    data.act = "Find_StateCount";
    app.Ajax(data, function (json) {
      that.setData({state:json.data});
    });
  },
  openUser:function(){
    wx.navigateTo({
      url: '/pages/user/user'
    })
  },
  onGotUserInfo: function (e) {
    var that = this;
    util.Loading("登陆中...");
    var user = e.detail;
    wx.login({
      success: function (res) {
        if (res.code) {
          var data = {};
          data.code = res.code;
          data.realname = user.userInfo.nickName;
          data.headimg = user.userInfo.avatarUrl;
          data.sex = user.userInfo.gender;
          data.province = user.userInfo.province;
          data.city = user.userInfo.city;
          data.act = "Login";
          app.Ajax(data, function (json) {
            app.User = json.data;
            wx.setStorageSync("userid", json.data.UserID);
            that.Find_UserInfo();
          });
        } else {
          util.Alert('获取用户登录态失败！');
        }
      },
      complete: function () {
        wx.hideLoading();
      }

    });
  },
  Find_UserInfo: function () {
    var that = this;
    var data = {};
    data.act = "Find_UserInfo";
    util.Loading("加载中...");
    app.Ajax(data, function (json) {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      that.setData({
        user: json.data
      });
      app.User = json.data;
    });
  }
})